package com.suse.warmstill.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.suse.warmstill.common.enums.RedisEnum;
import com.suse.warmstill.common.model.CallResult;
import com.suse.warmstill.entity.TbIndexPicture;
import com.suse.warmstill.mapper.TbArticleMapper;
import com.suse.warmstill.mapper.TbIndexPictureMapper;
import com.suse.warmstill.service.IndexPictureService;
import com.suse.warmstill.vo.IndexPicturePostVo;
import com.suse.warmstill.vo.IndexPitureVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 黄靖
 * @author 2022-11-05 11:31
 * description:
 **/
@Api(tags = "pc端 轮播图的管理")
@RestController("/pc/indexPicture")
public class PcIndexPicture {
    @Resource
    private TbArticleMapper articleMapper;


    @Resource
    private IndexPictureService indexPictureService;

    @Resource
    TbIndexPictureMapper indexPictureMapper;

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @ApiOperation("拿到所有的 图片资源")
    @GetMapping("/list")
    public CallResult listAll() {
        List<IndexPitureVO> list = indexPictureService.listAll();
        return CallResult.success(list);
    }

    @ApiOperation("插入一张图")
    @PostMapping("/insert")
    public CallResult insert(@RequestBody IndexPicturePostVo indexPicture) {

        final TbIndexPicture tbIndexPicture= new TbIndexPicture();

       tbIndexPicture.setPictureAdress(indexPicture.getPictureAdress());
       tbIndexPicture.setPath(indexPicture.getPath());
       tbIndexPicture.setName(indexPicture.getName());
       tbIndexPicture.setContent(indexPicture.getContent());
       tbIndexPicture.setAvator(indexPicture.getAvator());
    tbIndexPicture.setReateTime(LocalDateTime.now());
        indexPictureMapper.insert(tbIndexPicture);
        redisTemplate.delete(RedisEnum.INDEX_PICURE);
        return CallResult.success();
    }



    @ApiOperation("拿到所有的文章")
    @GetMapping
    public CallResult getAllArticle(){
        return CallResult.success( articleMapper.selectList(new LambdaQueryWrapper<>()));
    }

    @ApiOperation("删除一张")
    @GetMapping("/deleteOne/{id}")
    private CallResult deleteOne(@PathVariable("id") Long id) {
        indexPictureMapper.deleteById(id);
        redisTemplate.delete(RedisEnum.INDEX_PICURE);
        return CallResult.success();
    }

    @PostMapping("/updateOne")
    @ApiOperation("修改一张，id必须要")
    private CallResult updateOne(@RequestBody  TbIndexPicture indexPicture) {
        indexPicture.setReateTime(LocalDateTime.now());
        indexPictureMapper.updateById(indexPicture);
        redisTemplate.delete(RedisEnum.INDEX_PICURE);
        return CallResult.success();
    }


}
